package com.zmn.oms.task.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.manager.mq.annotation.AliyunMQHandler;
import com.zmn.manager.mq.annotation.AliyunMQListener;
import com.zmn.oms.common.annotation.OrderWorkChangeSync;
import com.zmn.oms.common.constant.MessageQueueTagConsts;
import com.zmn.oms.manager.utils.OmsMqTagConsts;
import com.zmn.oms.model.dio.part.OrderPartReviewDIO;
import com.zmn.oms.model.dto.part.PartReimburseDTO;
import com.zmn.oms.services.interfaces.datasync.ElasticSearchDataSyncService;
import com.zmn.oms.services.interfaces.order.OrderChangeService;
import com.zmn.oms.zmn.normal.business.interfaces.part.ZsNormalOrderPartBService;
import com.zmn.scm2.common.model.dro.scm.reimburse.ScmReimburseDRO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * 类描述：配件报销
 *
 * @author wangxiaokun
 * @date 2020/10/14 16:53
 */
@AliyunMQListener(topic = "${spring.mq.topic.scm2}")
@Component
public class OrderPartReimburseListener {

	private static final Logger logger = LoggerFactory.getLogger(OrderPartReimburseListener.class);

	@Autowired
	private ZsNormalOrderPartBService zsNormalOrderPartBService;
	@Autowired
	private ElasticSearchDataSyncService elasticSearchDataSyncService;

	/**
	 * 报销处理
	 * @param message 
	 * @return void
	 * @author wangxiaokun
	 */
	@AliyunMQHandler(tags = OmsMqTagConsts.SCM_REIMBURSE_MQ_TAGS)
	public void scmReimburse(String message){
		try{
			logger.info("#mq#reimburse#orderpart#recive message:{}", message);
			ScmReimburseDRO scmReimburseDRO = JSON.parseObject(message, ScmReimburseDRO.class);
			PartReimburseDTO partReimburseDTO = BeanMapper.map(scmReimburseDRO, PartReimburseDTO.class);
			zsNormalOrderPartBService.insertReimburseOrderPart(partReimburseDTO);
		} catch (Exception e){
			logger.info("#mq#reimburse#orderpart#error:{}", e.getMessage());
			e.printStackTrace();
		}
	}

	/**
	 * 取消报销
	 * @param message
	 * @return void
	 * @author wangxiaokun
	 */
	@AliyunMQHandler(tags = OmsMqTagConsts.SCM_CANCEL_REIMBURSE_MQ_TAGS)
	public void scmCancelReimburse(String message){
		try{
			logger.info("#mq#cancel#reimburse#orderpart#recive message:{}", message);
			JSONObject jsonObject = JSONObject.parseObject(message);
			zsNormalOrderPartBService.clearOrderPartByOrderIdAndReimburseId(jsonObject.getLong("orderId"),
					jsonObject.getString("reimburseId"));
		} catch (Exception e){
			logger.info("#mq#cancel#reimburse#orderpart#error:{}", e.getMessage());
			e.printStackTrace();
		}
	}

	/**
	 * 取消报销
	 * @param message
	 * @return void
	 * @author wangxiaokun
	 */
	@AliyunMQHandler(tags = MessageQueueTagConsts.ORDER_PART_REVIEW_TAG)
	public void scmPartReview(String message){
		try{
			logger.info("#mq#orderpartreview#recive message:{}", message);
			OrderPartReviewDIO orderPartReviewDIO = JSONObject.parseObject(message, OrderPartReviewDIO.class);
			zsNormalOrderPartBService.modifyOrderPartReview(orderPartReviewDIO);
			elasticSearchDataSyncService.syncWorkIndex(orderPartReviewDIO.getWorkId());
		} catch (Exception e){
			logger.info("#mq#orderpartreview#error:{}", e.getMessage());
			e.printStackTrace();
		}
	}
}
